package com.seu.dm.controllers;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seu.dm.annotations.permissions.SellerPermission;
import com.seu.dm.dto.UserBaseDTO;
import com.seu.dm.entities.Picture;
import com.seu.dm.entities.Product;
import com.seu.dm.entities.Seller;
import com.seu.dm.helpers.FileUploadHelper;
import com.seu.dm.helpers.PageGenerateHelper;
import com.seu.dm.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Greeting on 2017/2/28.
 * 商品相关的控制层逻辑，根据方法使用者的身份添加不同的权限
 * SellerPermission,BuyerPermission,游客默认无权限
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private CampusService campusService;
    @Autowired
    private FavoriteService favoriteService;

    /**
     * 根据商品名找到相关信息并用特定实体包装传入model
     * @param name
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchGood",method = RequestMethod.GET)
    public String searchEntityByName(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "search")String name,Model model,
                                     HttpServletRequest request,
                                     HttpSession httpSession){
        PageHelper.startPage(pageNum,10);
        List<Product> products = productService.findProductsByName(name);
        PageInfo pageInfo = new PageInfo(products);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        System.out.println(products.size());
//        List<SearchGoodEntity> searchGoodEntities = productService.searchEntitiesByName(name);
//        model.addAttribute("entities",searchGoodEntities);
        model.addAttribute("entities",products);
        httpSession.setAttribute("name",name);
        return "product/product_list";
    }

    /**
     * 根据价格区间筛选商品，并将查询结果放入model中传入前台
     * @param minPrice
     * @param maxPrice
     * @param model
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/screenByPrice", method = RequestMethod.GET)
    public String screenByPrice(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                HttpServletRequest request,
                                @RequestParam(value = "minPrice")Double minPrice,
                                @RequestParam(value = "maxPrice")Double maxPrice,Model model,HttpSession httpSession){
        String name = (String) httpSession.getAttribute("name");
        System.out.println("s" + name);
        PageHelper.startPage(pageNum,10);
        List<Product> products = productService.findProductsByNameAndScreenByPrice(name,minPrice,maxPrice);
        PageInfo pageInfo = new PageInfo(products);
        PageGenerateHelper.generatePage(request,model,pageInfo);
        System.out.println(products.size());
//        httpSession.setAttribute("products",products);
        model.addAttribute("entities",products);
        return "product/product_list";
    }

    /**
     * 进入商品详情页面
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping(value = "/product_detail/{productId}")
    public String productDetail(@PathVariable Integer productId,Model model,HttpServletRequest request){
        System.out.println(productId);

        Product product = productService.findProduct(productId);
        Seller seller = sellerService.findSeller(product.getSellerId());
        String campus = campusService.findCampus(seller.getCampusId()).getName();


        model.addAttribute("product",product);
        model.addAttribute("seller",seller);
        model.addAttribute("campus",campus);

        return "product/product_details";
    }
    /**
     * 根据ID找到指定商品
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/search/{id}")
    public String findProduct(@PathVariable Integer id,Model model){
        Product product =productService.findProduct(id);
        Seller seller = sellerService.findSeller(product.getSellerId());
        Integer campusId = seller.getCampusId();
        System.out.println("campusId"+campusId);
        String campus = campusService.findCampus(campusId).getName();
        model.addAttribute("product",product);
        model.addAttribute("campus",campus);
        System.out.println("OK");
        return "productDetails";
    }

    /**
     * 根据商品名模糊查询找到相关商品列表
     * @param name
     * @param model
     * @return
     */
    @RequestMapping(value = "/findByName", method = RequestMethod.GET)
    public String findProductsByName(@RequestParam(value = "name")String name,Model model){
        List<Product> products = productService.findProductsByName(name);
        for (Product product : products) {
            System.out.println(product.getName());
        }
        model.addAttribute("products",products);
       // System.out.println("ok");
        return "productDetails";
    }

    /**
     * 根据商品名找到对应商品
     * @param name
     * @param model
     * @return
     */
    @RequestMapping(value = "/search/{name}")
    public String searchProductByName(@PathVariable String name,Model model){
        Product product =productService.findProductByName(name);
        Seller seller = sellerService.findSeller(product.getSellerId());
        Integer campusId = seller.getCampusId();
        System.out.println("campusId"+campusId);
        String campus = campusService.findCampus(campusId).getName();
        model.addAttribute("product",product);
        model.addAttribute("campus",campus);
        System.out.println("OK");
        return "productDetails";
    }

    /**
     * 根据商品名找到对应商品
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping(value = "searchbykeyword/{keyword}")
    public String searchProductsByKeyword(@PathVariable String keyword,HttpSession httpSession,Model model){
        System.out.println(keyword);
        List<Product> products = productService.findProductsByKeyword(keyword);
        model.addAttribute("entities",products);
        httpSession.setAttribute("name",keyword);
        return "product/product_list";
    }

    /**
     * 根据标签返回对应的商品列表
     * @param category
     * @param model
     * @return
     */
    @RequestMapping(value = "/findByCategory", method = RequestMethod.GET)
    public String findProductsByCategory(@RequestParam(value = "category")String category, Model model){
        List<Product> products = productService.findProductsByCategory(category);
        model.addAttribute("products",products);
        return "";
    }

    /**
     * 根据ID得到商品对应的价格
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/getPrice")
    public String getProductPriceById(@RequestParam(value = "id")Integer id, Model model){
        BigDecimal price = productService.getProductPriceById(id);
        System.out.println(price);
        model.addAttribute("price",price);
        return "productDetails";
    }


    /**
     * 根据商品名得到模糊查询的结果数
     * @param name
     * @param model
     * @return
     */
    @RequestMapping(value = "/getCount")
    public String getCounts(@RequestParam(value = "name")String name, Model model){
        int i = productService.getCountOfResultsByName(name);
        System.out.println("find"+i+"messages");
        return "demo/helloworld";
    }


    /**
     * 添加商品
     * @param product
     * @param model
     * @return
     */
    @RequestMapping(value = "/addProduct")
    @SellerPermission
    public String addProduct(Product product, HttpSession httpSession, HttpServletRequest request,Model model)throws IOException{
        Integer sellerId = ((UserBaseDTO)httpSession.getAttribute("userBase")).getId();
        product.setSellerId(sellerId);
        product.setIsShelf(true);
        Picture picture = new Picture();
        byte[] pictureBinary = (FileUploadHelper.uploadPicture(request,"picture"));
        picture.setBinaryFile(pictureBinary);
        pictureService.addPicture(picture);
        Integer pictureId = picture.getId();
        product.setPictureId(pictureId);
        int i = productService.addProduct(product);
        model.addAttribute("product",product);
        return "seller/new_products";
    }


    /**
     * 根据ID删除指定商品
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/deleteProduct/{id}")
    @SellerPermission
    public String deleteProduct(@PathVariable Integer id, Model model){
        int i = productService.deleteProduct(id);
        return "/";
    }


    /**
     * 更新商品信息
     * @param product
     * @param model
     * @return
     */
    @RequestMapping(value = "updateProduct")
    @SellerPermission
    public String updateProduct(Product product, Model model){
        int i = productService.updateProduct(product);
        if(i == 1) System.out.println("update success");
        System.out.println("update failed");
        return "";
    }


}
